#include "widget.h"
#include "ui_widget.h"
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    file.setFileName("/dev/100ask_led");
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked(bool checked)
{
    qDebug() << checked << endl;
    if(!file.exists()){
        qDebug() << "LED不存在！" << endl;
        return;
    }

    if(!file.open(QIODevice::WriteOnly))
        return;

    char buf[] = {0,1};

    if(checked){
        ui->pushButton->setText("关闭");
        buf[1] = 0;
        file.write(buf,2);
        qDebug() << " file.write(&buf[1]) = " << buf[1] << endl;
    }
    else{
        ui->pushButton->setText("打开");
        buf[1] = 1;
        file.write(buf,2);
        qDebug() << " file.write(&buf[0]) = " << buf[0] << endl;
    }

    file.close();
}

void Widget::keyPressEvent(QKeyEvent *event)
{
    if(event->key() == Qt::Key_VolumeDown) { //检测是否按下 QT有对Key_VolumeDown等匹配的键值 帮助中查看
        this->setStyleSheet("QWidget {background-color:red}"); //改变主窗口的颜色
        qDebug() << "音量减键按下" << endl;
    }
    qDebug() << "有按键按下" << endl;
}

void Widget::keyReleaseEvent(QKeyEvent *event)
{
    if(event->key() == Qt::Key_VolumeDown) { //检测是否按下 QT有对Key_VolumeDown等匹配的键值 帮助中查看
        this->setStyleSheet("QWidget {background-color:white}"); //改变主窗口的颜色
        qDebug() << "音量减键松开" << endl;
    }
    qDebug() << "有按键松开" << endl;
}




